package com.indusfo.spc.untlis;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具类
 * 
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{

    public static final Integer HOUR=24;
    public static final Integer MINUTE=60;
    public static final Integer SECOND=60;
    public static final Integer MILLISECOND=1000;

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String MM_DD = "MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String HH_MM_SS = "HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }


    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 对日期进行天数的处理
     * @param firstDate
     * @param preiod
     * @return
     */
    public static String getSimpleDate(String firstDate,Integer preiod){
            try{
                Date date = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(firstDate);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(date);
                Integer month=Integer.parseInt(firstDate.split(" ")[0].split("-")[1])+preiod;
                if(month>12){
                    int year=month/12;
                    rightNow.add(Calendar.YEAR,year);//日期减1年

                    rightNow.add(Calendar.MONTH,month%12);//日期加3个月
                }else{
                    rightNow.add(Calendar.MONTH,month);//日期加3个月
                }
                Date simpleDate=rightNow.getTime();
                String simpleDateStr = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,simpleDate);
                return simpleDateStr;
            }catch (ParseException e){
                return e.getMessage();
            }
    }

    /**
     * 对日期进行小时的加减
     * @param startDate
     * @param workhour
     * @return
     */
    public static String getSimpleWorkhour(String startDate,double workhour){
        String[] split = startDate.split(":");

        long round = Math.round(workhour * 100);
        int min = (int)round%100;
        int hour=(int)workhour;
        if(min!=0){
            if(Integer.parseInt(split[1])+min>=60){
                hour+=1;
                split[1]=String.valueOf(Integer.parseInt(split[1])+min-60);
            }else{
                split[1]=String.valueOf(Integer.parseInt(split[1])+min);
            }
        }
        if(hour!=0){
            split[0]=String.valueOf(Integer.parseInt(split[0])+hour);
        }
        StringBuffer sb=new StringBuffer();
        for (String s : split) {
            sb.append(s+":");
        }
        return sb.substring(0,sb.length()-1);
    }

    /**
     * 日期比较
     * @param startDate
     * @param endDate
     * @return
     */
    public static  int compareDate(String startDate,String endDate){
        try {
            Date start = new SimpleDateFormat(HH_MM_SS).parse(startDate);
            Date end = new SimpleDateFormat(HH_MM_SS).parse(endDate);
            return start.compareTo(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 日期转换
     * @param format
     * @param date
     * @return
     */
    public  static String transformDate(String format,String date){
        Date parseDate = null;
        try {
            parseDate = new SimpleDateFormat(format).parse(date);
            String transformDate = new SimpleDateFormat(format).format(parseDate);
            return transformDate;
        } catch (ParseException e) {
            return e.getMessage();

        }

    }

    /**
     * 验证日期格式
     * @param str
     * @return
     */
    public static boolean isValidDate(String str,String dateType) {
        boolean convertSuccess=true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat(dateType);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess=false;
        }
        return convertSuccess;
    }


    public static String changeDateDay(String endDate,Integer dayNumber){

        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
            Date simpleEndDate = simpleDateFormat.parse(endDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(simpleEndDate);
            calendar.add(Calendar.DATE, -dayNumber);
            Date date = calendar.getTime();
            String startDate = simpleDateFormat.format(date);
            return startDate;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 生成当天的一个时间戳
     * @param hourTime
     * @return
     */
    public  static String dateSplicer(String hourTime){
        StringBuffer simpleDate=new StringBuffer();
        String dateTimeNow = DateUtils.dateTimeNow(YYYY_MM_DD);
        simpleDate.append(dateTimeNow + " " + hourTime);
        return simpleDate.toString();
    }
    public  static String dateSplicer(String day,String hourTime){
        StringBuffer simpleDate=new StringBuffer();
        simpleDate.append(day + " " + hourTime);
        return simpleDate.toString();
    }
     public static String dateByDay(int amount){
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
         Calendar calendar = new GregorianCalendar();
         calendar.setTime(new Date());
         calendar.add(calendar.DATE,amount);
         String dateByDay= simpleDateFormat.format(calendar.getTime());
         return dateByDay;
     }
    public static List<String> queryMonthDateByNow(){
        List<String> monthDate=new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        for(int i=29;i>=1;i--){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -i);
            Date d = calendar.getTime();
            String day = format.format(d);
            monthDate.add(day);
        }
        monthDate.add(DateUtils.dateTimeNow(MM_DD));
        return monthDate;
       /* List<String> monthDate=new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        String nowDateString = simpleDateFormat.format(date);
        try {
            Date nowDate = simpleDateFormat.parse("2019-12-30");
            long time = nowDate.getTime();
            for(int i=30;i<=i;i--){
                String startDate = simpleDateFormat.format(nowDate.getTime() - (i * HOUR * MINUTE * SECOND * MILLISECOND));
                monthDate.add(startDate);
            }
            monthDate.add(nowDateString);
            return monthDate;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }*/
    }
}
